﻿Imports System.ServiceModel.DomainServices.Client.ApplicationServices
Imports LullabyFullDeCaixa.My.Resources

Partial Public Class Login
    Inherits Page

    Public Event Login()

    Public Sub New()
        InitializeComponent()
    End Sub

    'Executes when the user navigates to this page.
    Protected Overrides Sub OnNavigatedTo(ByVal e As System.Windows.Navigation.NavigationEventArgs)

    End Sub

    Private Sub LoginUserNameBox_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs)
        Me.loginButton.IsEnabled = (Me.loginUserNameBox.Text.Length > 0 AndAlso Me.loginPasswordBox.Password.Length > 0)
    End Sub

    Private Sub LoginPasswordBox_PasswordChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.loginButton.IsEnabled = (Me.loginUserNameBox.Text.Length > 0 AndAlso Me.loginPasswordBox.Password.Length > 0)
    End Sub

    Private Sub LoginButton_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
        If Contexte.UsuariActual IsNot Nothing Then
            Contexte.Finalitzar()
        End If

        Dim Param As LoginParameters = New LoginParameters(Me.loginUserNameBox.Text, Me.loginPasswordBox.Password, False, App.IpAddress)
        Dim LogOperation As LoginOperation = DirectCast(RiaContext.Current.Authentication, FormsAuthentication).Login(Param, New Action(Of LoginOperation)(AddressOf LoginOperation_Complete), Nothing)

        Me.Activity.IsActive = True
    End Sub

    Private Sub LoginOperation_Complete(ByVal LogOperation As LoginOperation)
        Dim MissatgeError As String = ""

        If LogOperation.HasError Then
            'Hi ha hagut una excepcio
            Me.Activity.IsActive = False
            Deployment.Current.Dispatcher.BeginInvoke(New Action(Of String)(AddressOf App.ReportError), StringLibrary.Login_LoginOperation_Complete_Se_ha_producido_un_error_iniciando_la_sesión__Revise_su_conexión_a_internet)
            Return

        ElseIf LogOperation.LoginSuccess Then
            If DirectCast(LogOperation.User, LullabyFullDeCaixa.Web.User).hasIpPermision Then
                'Obtinc l'usuari actual
                AddHandler Contexte.ContexteInicialitzat, AddressOf ContexteInicialitzat
                Contexte.Init(Me.loginUserNameBox.Text, Me.loginPasswordBox.Password)
                Return
            Else
                MissatgeError = StringLibrary.Login_Ubicación_incorrecta
            End If
        Else
            'Error de login password
            MissatgeError = StringLibrary.Login_LoginOperation_Complete_Datos_incorrectos
        End If

        Me.Activity.IsActive = False
        Me.Validacio.Errors.Clear()

        Dim ValidationItem As New ValidationSummaryItem()
        ValidationItem.Message = MissatgeError
        ValidationItem.ItemType = ValidationSummaryItemType.ObjectError
        Me.Validacio.Errors.Add(ValidationItem)

    End Sub

    Private Sub loginPasswordBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
        If e.Key = Key.Enter Then
            LoginButton_Click(Me, New RoutedEventArgs)
        End If
    End Sub

    Private Sub ContexteInicialitzat(ByVal Resultat As Boolean, ByVal Missatge As String)
        Me.Activity.IsActive = False
        RemoveHandler Contexte.ContexteInicialitzat, AddressOf ContexteInicialitzat

        'Aconseguim el idioma i el canviem...FUNCIONA
        My.Resources.StringLibrary.Culture = New Globalization.CultureInfo(Contexte.UsuariActual.Idioma)
        'LullabyFullDeCaixa.Web.My.Resources.StringLibrary.Culture = New Globalization.CultureInfo(Contexte.UsuariActual.Idiom)
        LullabyControls.My.Resources.StringLibrary.Culture = New Globalization.CultureInfo(Contexte.UsuariActual.Idioma)
        System.Threading.Thread.CurrentThread.CurrentUICulture = New Globalization.CultureInfo(Contexte.UsuariActual.Idioma)

        If Resultat Then
            Deployment.Current.Dispatcher.BeginInvoke(New Action(Of Uri)(AddressOf DirectCast(App.Current, App).Navigate), New Uri("/Home", UriKind.Relative))
        Else
            Deployment.Current.Dispatcher.BeginInvoke(New Action(Of String)(AddressOf App.ReportError), StringLibrary.ErrorCarregantDades)
        End If
    End Sub

	Private Sub Login_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Title = StringLibrary.Login_Page + " - " + StringLibrary.Versio + " - " + App.IpAddress

		If App.Current.Host.Source.DnsSafeHost = "localhost" Then
			Me.loginUserNameBox.Text = "lmarzabal"
			Me.loginPasswordBox.Password = "lmarzabal"
			'LoginButton_Click(Me, New RoutedEventArgs)
		End If
	End Sub

   
End Class
